package com.xsimas.xyybackend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xsimas.xyybackend.domain.Contact;
import com.xsimas.xyybackend.dto.PageRequestDTO;
import com.xsimas.xyybackend.pojo.Result;
import com.xsimas.xyybackend.service.ContactService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author RAY_HUI
 * @date 2025-07-09
 * @time 18:40
 */

@RestController
@RequestMapping("/api/contact")
@Tag(name = "联系人管理接口", description = "提供联系人的增删查改及分页操作")
public class ContactController {

    @Autowired
    private ContactService contactService;

    /**
     * 分页查询联系人
     */
    @PostMapping("/page")
    @Operation(summary = "分页查询联系人数据", description = "根据页码和每页数量分页查询联系人信息")
    public Result<IPage<Contact>> getContactsByPage(@RequestBody PageRequestDTO pageRequest) {
        Page<Contact> page = new Page<>(pageRequest.getPageNum(), pageRequest.getPageSize());
        return Result.ok(contactService.page(page));
    }

    /**
     * 查询所有联系人
     */
    @GetMapping("/list")
    @Operation(summary = "获取所有联系人列表", description = "返回所有联系人记录")
    public Result<List<Contact>> getAllContacts() {
        return Result.ok(contactService.list());
    }

    /**
     * 根据ID查询联系人
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取单个联系人", description = "通过ID查询联系人详情")
    public Result<Contact> getContactById(
            @Parameter(description = "联系人ID", required = true)
            @PathVariable Long id) {
        return Result.ok(contactService.getById(id));
    }

    /**
     * 新增联系人
     */
    @PostMapping("/add")
    @Operation(summary = "新增一个联系人记录", description = "将传入的联系人对象保存到数据库")
    public Result<Boolean> createContact(
            @Parameter(description = "联系人实体对象", required = true)
            @RequestBody Contact contact) {
        boolean success = contactService.save(contact);
        return success ? Result.ok(success) : Result.error(400, "新增联系人失败");
    }

    /**
     * 更新联系人
     */
    @PutMapping("/update")
    @Operation(summary = "更新一个联系人记录", description = "根据ID更新联系人信息")
    public Result<Boolean> updateContact(
            @Parameter(description = "联系人实体对象", required = true)
            @RequestBody Contact contact) {
        boolean success = contactService.updateById(contact);
        return success ? Result.ok(success) : Result.error(400, "更新联系人失败");
    }

    /**
     * 删除联系人
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "根据ID删除一个联系人", description = "逻辑或物理删除指定ID的联系人")
    public Result<Boolean> deleteContact(
            @Parameter(description = "联系人ID", required = true)
            @PathVariable Long id) {
        boolean success = contactService.removeById(id);
        return success ? Result.ok(success) : Result.error(400, "删除联系人失败");
    }
}